package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.dav.Env;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.pool.ConnectionPoolManager;
import com.fr.data.pool.DBCPConnectionPoolAttr;
import com.fr.data.pool.MemoryConnection;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.util.Utils;
import java.sql.Connection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;

public class JDBCDatabaseConnection extends AbstractDatabaseConnection
{
  private String driver;
  private String url;
  private String user;
  private String password;
  private DBCPConnectionPoolAttr dbcpAttr;
  private boolean encryptPassword;

  public JDBCDatabaseConnection()
  {
    this("", "", "", "");
  }

  public JDBCDatabaseConnection(String paramString1, String paramString2, String paramString3, String paramString4)
  {
    this.driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    this.url = "";
    this.user = "";
    this.password = "";
    this.dbcpAttr = null;
    this.encryptPassword = true;
    setDriver(paramString1);
    setURL(paramString2);
    setUser(paramString3);
    setPassword(paramString4);
  }

  public String getDriver()
  {
    return this.driver;
  }

  public void setDriver(String paramString)
  {
    this.driver = paramString;
  }

  public String getURL()
  {
    return this.url;
  }

  private String getActualURL()
  {
    if (this.url != null)
      try
      {
        return TemplateUtils.render(this.url, new HashMap(this)
        {
          private final JDBCDatabaseConnection this$0;
        });
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    return this.url;
  }

  public void setURL(String paramString)
  {
    this.url = paramString;
  }

  public String getUser()
  {
    return this.user;
  }

  public void setUser(String paramString)
  {
    this.user = paramString;
  }

  public String getPassword()
  {
    return this.password;
  }

  public void setPassword(String paramString)
  {
    this.password = paramString;
  }

  public boolean isEncryptPassword()
  {
    return this.encryptPassword;
  }

  public void setEncryptPassword(boolean paramBoolean)
  {
    this.encryptPassword = paramBoolean;
  }

  public DBCPConnectionPoolAttr getDbcpAttr()
  {
    return this.dbcpAttr;
  }

  public void setDbcpAttr(DBCPConnectionPoolAttr paramDBCPConnectionPoolAttr)
  {
    this.dbcpAttr = paramDBCPConnectionPoolAttr;
  }

  public void testConnection()
    throws Exception
  {
    ConnectionPoolManager localConnectionPoolManager = ConnectionPoolManager.getConnectionPoolManager();
    DataSource localDataSource = localConnectionPoolManager.getDataSource(getDriver(), getActualURL(), getUser(), getPassword(), this.dbcpAttr);
    Connection localConnection = localDataSource.getConnection();
    localConnection.close();
  }

  public Connection createConnection()
    throws Exception
  {
    ConnectionPoolManager localConnectionPoolManager = ConnectionPoolManager.getConnectionPoolManager();
    DataSource localDataSource = localConnectionPoolManager.getDataSource(getDriver(), getActualURL(), getUser(), getPassword(), this.dbcpAttr);
    return new MemoryConnection(localDataSource.getConnection());
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof JDBCDatabaseConnection) && (super.equals(paramObject)) && (ComparatorUtils.equals(getURL(), ((JDBCDatabaseConnection)paramObject).getURL())) && (ComparatorUtils.equals(getDriver(), ((JDBCDatabaseConnection)paramObject).getDriver())) && (ComparatorUtils.equals(getUser(), ((JDBCDatabaseConnection)paramObject).getUser())) && (ComparatorUtils.equals(getPassword(), ((JDBCDatabaseConnection)paramObject).getPassword())) && (isEncryptPassword() == ((JDBCDatabaseConnection)paramObject).isEncryptPassword()));
  }

  public int hashCode()
  {
    int i = 0;
    i = 37 * i + getDriver().hashCode();
    i = 37 * i + getURL().hashCode();
    i = 37 * i + getUser().hashCode();
    i = 37 * i + getPassword().hashCode();
    i = 37 * i + ((isEncryptPassword()) ? 1 : 0);
    return i;
  }

  public String toString()
  {
    return "[JDBCDatabase][Driver:" + getDriver() + "][URL:" + getURL() + "]" + "[User:" + getUser() + "][Password:***]";
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if ((paramXMLableReader.isChildNode()) && ("JDBCDatabaseAttr".equals(paramXMLableReader.getTagName())))
    {
      String str;
      if ((str = paramXMLableReader.getAttr("driver")) != null)
        setDriver(str);
      if ((str = paramXMLableReader.getAttr("url")) != null)
        setURL(str);
      if ((str = paramXMLableReader.getAttr("user")) != null)
        setUser(str);
      if ((str = paramXMLableReader.getAttr("password")) != null)
        setPassword(ReportXMLUtils.unPasswordString(str));
      if ((str = paramXMLableReader.getAttr("encryptPassword")) != null)
        setEncryptPassword(Boolean.valueOf(str).booleanValue());
      if ((str = paramXMLableReader.getAttr("originalCharsetName")) != null)
        setOriginalCharsetName(str);
      if ((str = paramXMLableReader.getAttr("newCharsetName")) != null)
        setNewCharsetName(str);
      DBCPConnectionPoolAttr localDBCPConnectionPoolAttr = new DBCPConnectionPoolAttr();
      setDbcpAttr(localDBCPConnectionPoolAttr);
      paramXMLableReader.readXMLObject(localDBCPConnectionPoolAttr);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("JDBCDatabaseAttr").attr("url", getURL()).attr("driver", getDriver()).attr("user", getUser());
    if (isEncryptPassword())
      paramXMLPrintWriter.attr("password", ReportXMLUtils.passwordString(getPassword()));
    else
      paramXMLPrintWriter.attr("password", getPassword());
    paramXMLPrintWriter.attr("encryptPassword", isEncryptPassword());
    if (getDbcpAttr() != null)
      getDbcpAttr().writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }
}